﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace 数据导入导出
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Botton1_Click(object sender, EventArgs e)
        {
            if (ofdImport.ShowDialog() == DialogResult.OK)
            {
                using(FileStream fileStream = File.OpenRead(ofdImport.FileName))
                {
                    using (StreamReader streamReader = new StreamReader(fileStream))
                    {
                        string line = null;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            string[] strs = line.Split('|');
                            string name = strs[0];
                            int age = Convert.ToInt32(strs[1]);
                            string SqlStr = @"Data Source=.; Initial Catalog=Mydb_1;User ID=sa;Password=13817995048";
                            using (SqlConnection conn = new SqlConnection(SqlStr))
                            {
                                conn.Open();
                                using (SqlCommand cmd = conn.CreateCommand())
                                {
                                    cmd.CommandText = "insert into T_Vip(FName,FAge) values(@Name,@Age)";
                                    cmd.Parameters.Add(new SqlParameter("Name",name));
                                    cmd.Parameters.Add(new SqlParameter("Age",age));

                                }
                            }
                        }
                    }
                }
            }
            MessageBox.Show("导入成功！");
        }
    }
}
